package com.blinddate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.InterestMapper;
import com.blinddate.mapper.RelevancyMapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.dto.UserInfDto;
import com.blinddate.pojo.entity.Interest;
import com.blinddate.pojo.entity.Relevancy;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.service.UserInfService;
import com.blinddate.utils.AliOssUtil;
import com.blinddate.utils.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserInfServiceImpl extends ServiceImpl<UserInfMapper,UserInf> implements UserInfService {

    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RelevancyMapper relevancyMapper;
    
    @Autowired
    private InterestMapper interestMapper;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private WeChatUtil weChatUtil;


    private final static Integer PAGE_SIZE=10;

    public List<UserInfVO> getUserList(Long userId,Integer gender,String rand,Integer count,String place,Integer age){
        //通过随机种子获取用户列表
        List<UserInf> userInfList = userInfMapper.getList(gender,rand,count,place,userId,age);
        List<UserInfVO> userInfVOS = new ArrayList<>();
        for (UserInf userInf:userInfList) {
            //添加必要的数据  绿卡 vip  showvip 关联
            UserInfVO userInfVO1 = new UserInfVO();
            userInfVO1.setUserInf(userInf);
            UserInfVO userInfVO = showAllInf(userInfVO1, userId, userInf.getUserId());
            userInfVOS.add(userInfVO);
        }
        return userInfVOS;
    }

    public List<UserInfVO> getOtherList(Long userId,Integer gender,String rand,Integer count,String place,String market,Integer age){
        List<UserInf> otherList = userInfMapper.getOtherList(gender, rand, count, place, market,userId,age);
        List<UserInfVO> userInfVOS = new ArrayList<>();
        for (UserInf userInf:otherList) {
            //添加必要的数据  绿卡 vip  showvip 关联
            UserInfVO userInfVO1 = new UserInfVO();
            userInfVO1.setUserInf(userInf);
            UserInfVO userInfVO = showAllInf(userInfVO1, userId, userInf.getUserId());
            userInfVOS.add(userInfVO);
        }
        return userInfVOS;

    }

    public UserInfVO getOneUser(Long userId,Long getId){
//        获取指定用户数据
        UserInf userInf = getUserInf(getId);
        UserInfVO userInfVO =new UserInfVO();
        userInfVO.setUserInf(userInf);
//       判断是否全部展示用户的数据
        userInfVO = showAllInf(userInfVO,userId,getId);
        return userInfVO;
    }

//     获取用户数据的函数  使用到缓存
    public UserInf getUserInf(Long userId){
        LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfLambdaQueryWrapper.eq(UserInf::getUserId,userId);
        UserInf userInf = userInfMapper.selectOne(userInfLambdaQueryWrapper);
        User user = userMapper.selectById(userId);
        log.info("user信息:{}",user);
        //添加绿卡信息
        if (user!=null){
            userInf.setGreenStartTime(user.getGreenStartTime());
            userInf.setGreenEndTime(user.getGreenEndTime());
        }
        // log.info("{}",userId);
        if (userInf.getGreenStartTime()==null || userInf.getGreenEndTime()==null || LocalDateTime.now().isAfter(userInf.getGreenEndTime())){
            userInf.setGreen_vip(0);//添加绿卡信息
        }if (user==null||user.getVipTimeStart()==null || user.getVipTimeEnd()==null || LocalDateTime.now().isAfter(user.getVipTimeEnd())){
            userInf.setVip(0);//添加vip信息
        }
        if (user!=null){
            userInf.setVipTimeEnd(user.getVipTimeEnd());
        }
        return userInf;
    }

    //    是否展示用户所有信息
    public UserInfVO showAllInf(UserInfVO userInfVO, Long userId, Long getId){
        //获取用户关联的relevancy
        LambdaQueryWrapper<Relevancy> relevancyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        relevancyLambdaQueryWrapper.eq(Relevancy::getUserId,userId).eq(Relevancy::getGetId,getId);
        Relevancy relevancy = relevancyMapper.selectOne(relevancyLambdaQueryWrapper);
        //获取用户的interest
        LambdaQueryWrapper<Interest> interestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        interestLambdaQueryWrapper.eq(Interest::getUserId,userId).eq(Interest::getGetId,getId);
        Interest interest = interestMapper.selectOne(interestLambdaQueryWrapper);
        User user = userMapper.selectById(getId);
        log.info("user信息:{}",user);
        //添加绿卡信息
        if (user!=null){
            userInfVO.getUserInf().setGreenStartTime(user.getGreenStartTime());
            userInfVO.getUserInf().setGreenEndTime(user.getGreenStartTime());
        }

        if (relevancy != null){
            userInfVO.setShowAll(1);
        }
        if (interest != null){
            userInfVO.setInterest(1);
        }
        if (userInfVO.getUserInf().getGreenStartTime()==null || userInfVO.getUserInf().getGreenEndTime()==null ||LocalDateTime.now().isAfter(userInfVO.getUserInf().getGreenEndTime())){
            userInfVO.getUserInf().setGreen_vip(0);
        }if (user==null || user.getVipTimeStart()==null || user.getVipTimeEnd()==null || LocalDateTime.now().isAfter(user.getVipTimeEnd())){
            userInfVO.getUserInf().setVip(0);
        }
        return userInfVO;
    }

    public Integer uploaduserInf(UserInfDto userInfDto){
        UserInf userInf=new UserInf();
//        判断是否存在该用户信息
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfLambdaQueryWrapper.eq(UserInf::getUserId,userId);
        UserInf user = userInfMapper.selectOne(userInfLambdaQueryWrapper);
        if (user != null){
            return 0;
        }

        BeanUtils.copyProperties(userInfDto,userInf);
        userInf.setUserId(userId);
        userInf.setCodeSrc(weChatUtil.getwxcode(userId));
        int insert = userInfMapper.insert(userInf);
        return insert;
    }


    /**
     * 解锁用户信息
     *
     * @param id
     * @return
     */
    public UserInfVO unlock(Long id) {//解锁
        Long userId = BaseContext.getCurrentId();
        Relevancy relevancy=new Relevancy();
        //查询是否已经解锁该用户
        LambdaQueryWrapper<Relevancy> rylqw=new LambdaQueryWrapper<>();
        rylqw.eq(Relevancy::getUserId,userId).eq(Relevancy::getGetId,id);
        relevancy=relevancyMapper.selectOne(rylqw);
        if(relevancy!=null){
            return null;
        }
        User user=userMapper.selectById(userId);
        Integer count =user.getCount();
        UserInfVO userInfVO=new UserInfVO();
        if(count<1){
            return null;
        }else{
            relevancy=new Relevancy();


            relevancy.setUserId(BaseContext.getCurrentId());
            relevancy.setGetId(id);
            relevancyMapper.insert(relevancy);
            count--;
            user.setCount(count);
            userMapper.updateById(user);
        }
        LambdaQueryWrapper<UserInf> lqw=new LambdaQueryWrapper<UserInf>();
        lqw.eq(UserInf::getUserId,id);
        UserInf userInf=userInfMapper.selectOne(lqw);
        userInfVO.setUserInf(userInf);
        userInfVO.setShowAll(1);
        return userInfVO;
    }


    public void wxupload(String filePath) {
        LambdaUpdateWrapper<UserInf> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(UserInf::getUserId,BaseContext.getCurrentId());
        UserInf userInf=userInfMapper.selectOne(lqw);
        userInf.setHeadSrc(filePath);
        userInfMapper.updateById(userInf);
    }

    @Override
    public Boolean deleteUser(Long userId) {
        LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfLambdaQueryWrapper.eq(UserInf::getUserId,userId);
        int delete = userInfMapper.delete(userInfLambdaQueryWrapper);
        //删除谁收藏我 我收藏谁  不管有没有都删除一次
        LambdaQueryWrapper<Relevancy> relevancyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        relevancyLambdaQueryWrapper.eq(Relevancy::getUserId,userId).or().eq(Relevancy::getGetId,userId);
        int delete1 = relevancyMapper.delete(relevancyLambdaQueryWrapper);
        //删除我看过谁 谁看过我  不管有没有都删除一次
        LambdaQueryWrapper<Interest> interestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        interestLambdaQueryWrapper.eq(Interest::getUserId,userId).or().eq(Interest::getGetId,userId);
        int delete2 = interestMapper.delete(interestLambdaQueryWrapper);

        if (delete==1){
            return true;
        }
        return false;
    }

    @Override
    public Integer getUserGender(Long userId) {
        Integer gender = userInfMapper.getGender(userId);
        return gender;
    }
}
